function createDatabase
%with label me
addpath('.\LectureDirigee','-begin') ;
addpath('.\LabelMeToolbox','-begin') ;

annotationsDir = 'D:\Usagers\matre697\Data.nobkp\Database\LMAnnotations';
imagesDir = 'D:\Usagers\matre697\Data.nobkp\Database\LMImages';
dbname = 'labelMeDB_2012';

newDir1 = 'D:\Usagers\matre697\Data.nobkp\Database\LMCarSide\';
newDir2 = 'D:\Usagers\matre697\Data.nobkp\Database\LMCarRear\';
newDir3 = 'D:\Usagers\matre697\Data.nobkp\Database\LMCarFront\';
newDir4 = 'D:\Usagers\matre697\Data.nobkp\Database\LMCar\';
newDirNeg = 'D:\Usagers\matre697\Data.nobkp\Database\LMNegatives\';

mkdir(newDir1);
mkdir(newDir2);
mkdir(newDir3);
mkdir(newDir4);
mkdir(newDirNeg);
mkdir(imagesDir);

[isThere LabelMe] = ifFileExistThenLoad(dbname);
if(isThere==0)
    % This line reads the entire database into a Matlab struct
    database = LMdatabase(annotationsDir);
    save(dbname,'database');
else
    database = LabelMe.database;
end

offsetRatio = [0.04 0.175];

% % Queries for objects
[Dcs,idx1] = LMquery(database, 'object.name', 'car+side');
extractImage(imagesDir,newDir1,'car+side',database,idx1,offsetRatio);
clear Dcs;
clear idx1;
[Dcr,idx2] = LMquery(database, 'object.name', 'car+rear');
extractImage(imagesDir,newDir2,'car+rear',database,idx2,offsetRatio);
clear Dcr;
clear idx2;
[Dcf,idx3] = LMquery(database, 'object.name', 'car+front');
extractImage(imagesDir,newDir3,'car+front',database,idx3,offsetRatio);
clear Dcf;
clear idx3;
[Dcf,idx4] = LMquery(database, 'object.name', 'car');
extractImage(imagesDir,newDir4,'car',database,idx4,offsetRatio);
clear Dcf;
clear idx4;

% Queries for objects
[DNeg,idxNeg] = LMquery(database, 'object.name', 'road-car');
%extractImage(imagesDir,newDirNeg,'-car',database,idxAll);
extractDB(imagesDir,newDirNeg,database,idxNeg);

function extractImage(imagesDir,newDir,objectType,database,idx,offsetRatio)

if(nargin == 5)
    offsetRatio = [0 0];
end

count = 1;

for i=1:length(idx)
    [annotation, img] = LMread(database,idx(i) ,imagesDir);
    idxObject = LMobjectindex(annotation, objectType);
    objects = annotation.object(idxObject);

    for j=1:length(objects)
        [x,y] = getLMpolygon(objects(j).polygon);
        right = uint16(max(x));
        top = uint16(max(y));
        left = uint16(min(x));
        bottom = uint16(min(y));
        width = abs(right - left);
        height = abs(top-bottom);
        right = right + width*offsetRatio(1);
        left = left - width*offsetRatio(1);
        top = top + height*offsetRatio(2);
        bottom = bottom - height*offsetRatio(2);
        if(left<1)
           left = 1;
        end
        if(left > right)
           continue;
        end
        if(bottom<1)
           bottom = 1;
        end
        if(bottom > top)
          continue; 
        end
        if(top > size(img,1))
          top = size(img,1);
        end
        if(right > size(img,2))
          right = size(img,2);
        end
        extractImg = img(bottom:top,left:right,:);
        temp = num2str(count, '%05i');
        filePath = [newDir,temp,'.bmp'];
        imwrite(extractImg,filePath);
        count = count + 1;
    end
end

function extractDB(imagesDir,newDir,database,idx)

for i=1:length(idx)
    [annotation, img] = LMread(database,idx(i) ,imagesDir);
    temp = num2str(i, '%05i');
    filePath = [newDir,temp,'.bmp'];
    imwrite(img,filePath);     
end